package cn.edu.hqu.exp.courseselection.web;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

import cn.edu.hqu.exp.courseselection.domain.Course;
import cn.edu.hqu.exp.courseselection.domain.Course.Type;
import cn.edu.hqu.exp.courseselection.repository.CourseRepository;
import cn.edu.hqu.exp.courseselection.repository.ElectionFormRepository;
import cn.edu.hqu.exp.courseselection.repository.PersonInfoRepository;
import cn.edu.hqu.exp.courseselection.domain.ElectionForm;
import cn.edu.hqu.exp.courseselection.domain.PersonInfo;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Controller
@RequestMapping("/course")
@SessionAttributes("personinfo")
public class CourseSeletionController {
	private final CourseRepository courseRepo;
	private ElectionFormRepository electionFormRepo;
	private PersonInfoRepository personInfoRepo;
	
	@Autowired
	public CourseSeletionController(
			CourseRepository courseRepo,
			ElectionFormRepository electionFormRepo,
			PersonInfoRepository personInfoRepo
			) {
		this.courseRepo = courseRepo;
		this.electionFormRepo = electionFormRepo;
		this.personInfoRepo = personInfoRepo;
	}
	
	@ModelAttribute
	public void setCourse(Model model) {
		List<Course> courses = new ArrayList<>();
		courseRepo.findAll().forEach(i -> courses.add(i));
		Type[] types = Course.Type.values();
		model.addAttribute("types", types);
		for (Type type : types) {
		  model.addAttribute(type.toString(),
		      filterByType(courses, type));
		}
	}
	
	@GetMapping
	public String course(Model model) {
		model.addAttribute("electionform", new ElectionForm());
		return "course";
	}
	
	@PostMapping
	public String  processCourse(@Valid @ModelAttribute("electionform") ElectionForm elections,Model model, Errors errors,@SessionAttribute("personinfo") PersonInfo personinfo,SessionStatus sessionStatus) {
		if(errors.hasErrors()) {
			return "course";
		}
		log.info("yunxian"+personinfo);
		
		ElectionForm ele = electionFormRepo.save(elections);
		log.info("课程: " + ele);
		
		personinfo.addElectionForm(ele);
		PersonInfo per = personInfoRepo.save(personinfo);
		log.info("个人信息: " + per);
		sessionStatus.setComplete();
		return "redirect:/conclude";
	}
	
	private List<Course> filterByType(List<Course> courses, Type type){
		return courses.stream()
				.filter(x -> x.getType().equals(type))
				.collect(Collectors.toList());
	}
}
